package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.TopicHistoryAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.RecyclerViewScrollListener
import com.gin.kotlin.no_gay_alliance.databinding.ActivityTopicHistoryBinding

class TopicHistoryActivity : AbstractBindingActivity<ActivityTopicHistoryBinding>(), TextView.OnEditorActionListener {

    private lateinit var adapter: TopicHistoryAdapter
    private var page = 1
    private var size = 20
    private var hasNext = true

    override fun onResume() {
        super.onResume()
        setToolbarTitle("主题浏览历史")
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.search, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.mi_search) newSearch()

        return super.onOptionsItemSelected(item)
    }

    override fun onCreate() {
        val b = this.b ?: return
        this.adapter = TopicHistoryAdapter(this, handler!!)
        b.recyclerView.adapter = adapter
        b.etKeyword.setOnEditorActionListener(this)
        b.swipeRefreshLayout.setOnRefreshListener { newSearch() }
        b.recyclerView.addOnScrollListener(RecyclerViewScrollListener(scrollToBottomListener = object : RecyclerViewScrollListener.ScrollToBottomListener {
            override fun onScrollToBottom() {
                if (hasNext) {
                    page++
                    toast("正在加载...")
                    request()
                } else {
                    toast("没有更多了...")
                }
            }
        }))

        newSearch()
    }

    private fun newSearch() {
        page = 1
        hasNext = true
        adapter.clear()
        uiDelayed(200){request()}
    }

    private fun request() {
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = true

        val dao = App.INSTANCE.database.topicHistoryDao()
        val keyword = b.etKeyword.text.toString().trim().takeIf { it.isNotEmpty() }
        Log.i(TAG, "查询: $keyword , page: $page")
        val list = dao.page(page, size,keyword)
        b.swipeRefreshLayout.isRefreshing = false
        if (list.isNullOrEmpty()) {
            hasNext = false
            toast("没有更多了...")
        }else {
            if (list.size < size) hasNext = false
            adapter.pushData(list)
        }
    }

    override fun handleIntent(intent: Intent) {
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        // 当按下软键盘搜索键时，执行搜索
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            newSearch()
            return true
        }

        return false
    }

    companion object{
        @JvmStatic
        fun start(context: Context) = context.startActivity(Intent(context, TopicHistoryActivity::class.java))
    }
}